//
//  KKMainViewController.swift
//  KKWeibo
//
//  Created by kassia on 16/7/9.
//  Copyright © 2016年 kassia. All rights reserved.
//

import UIKit
import SVProgressHUD
class KKMainViewController: UITabBarController {

    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.backgroundColor = UIColor.whiteColor()
        
        addChildViewController(KKHomeViewController(), title: "首页", imageName: "tabbar_home")
        
        addChildViewController(KKMessageViewController(), title: "消息", imageName: "tabbar_message_center")
        
        addChildViewController(KKDiscoverViewController(), title: "发现", imageName: "tabbar_discover")
        
        addChildViewController(KKProfileViewController(), title: "我的", imageName: "tabbar_profile")
        
        //使用kvc给只读属性赋值
        let tabBar = KKTabBar()
        setValue(tabBar, forKey: "tabBar")
        
    
        //撰写按钮闭包
        tabBar.composeButtonClosure = { [weak self] in
 
            print(" 我是闭包过来的")
            if !KKUserAccountModel.sharedUserAccount.isLogin {
            
                SVProgressHUD.showErrorWithStatus("请登录,亲~~")
            
                return 
            }
            
            if let weakSelf = self {
            
                // 代码执行到此已经登录
                let composeView = KKComposeView()
                
                composeView.show(weakSelf) 
            
            }
            
      
        }

    }

    func addChildViewController(childController: UIViewController, title: String, imageName: String) {//添加子控制器
        
        //设置自定义tabBarItem
        childController.tabBarItem = KKTabBarItem()
        
        childController.title = title //设置标题
        
        childController.tabBarItem.image = UIImage(named: imageName)//设置图片
        
        childController.tabBarItem.selectedImage = UIImage(named: imageName + "_selected")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)//修改选中时图片的渲染方式
        
        childController.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.orangeColor()], forState: .Selected)//设置字体颜色
        
        let nav = UINavigationController(rootViewController: childController)//创建导航控制器
        
        addChildViewController(nav)//添加
 
    }
}
